package com.gitease.quickhub.controller;

import com.gitease.quickhub.model.Commit;
import com.gitease.quickhub.model.Repository;
import com.gitease.quickhub.model.RepositoryFile;
import com.gitease.quickhub.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/repos")
public class RepositoryController {

    private final RepositoryService repositoryService;

    @Autowired
    public RepositoryController(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @GetMapping
    public ResponseEntity<List<Repository>> getUserRepositories(@RequestHeader("Authorization") String authHeader) {
        String accessToken = authHeader.replace("Bearer ", "");
        List<Repository> repositories = repositoryService.getUserRepositories(accessToken);
        return ResponseEntity.ok(repositories);
    }

    @GetMapping("/{owner}/{repo}")
    public ResponseEntity<Repository> getRepository(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo) {
        String accessToken = authHeader.replace("Bearer ", "");
        try {
            Repository repository = repositoryService.getRepository(accessToken, owner, repo);
            return ResponseEntity.ok(repository);
        } catch (RuntimeException e) {
            // 检查异常消息是否包含 "not found" 或 "Repository not found"
            if (e.getMessage() != null && e.getMessage().toLowerCase().contains("not found")) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
            }
            // 其他运行时异常返回 500 错误
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/{owner}/{repo}/contents")
    public ResponseEntity<List<RepositoryFile>> getRepositoryContents(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "ref", required = false) String ref) {
        String accessToken = authHeader.replace("Bearer ", "");
        List<RepositoryFile> contents = repositoryService.getRepositoryContents(accessToken, owner, repo, path, ref);
        return ResponseEntity.ok(contents);
    }

    @GetMapping("/{owner}/{repo}/contents/{path:.+}")
    public ResponseEntity<RepositoryFile> getRepositoryFile(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo,
            @PathVariable("path") String path,
            @RequestParam(value = "ref", required = false) String ref) {
        String accessToken = authHeader.replace("Bearer ", "");
        RepositoryFile file = repositoryService.getRepositoryFile(accessToken, owner, repo, path, ref);
        return ResponseEntity.ok(file);
    }

    @GetMapping("/{owner}/{repo}/commits")
    public ResponseEntity<List<Commit>> getRepositoryCommits(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo,
            @RequestParam(value = "branch", required = false) String branch) {
        String accessToken = authHeader.replace("Bearer ", "");
        List<Commit> commits = repositoryService.getRepositoryCommits(accessToken, owner, repo, branch);
        return ResponseEntity.ok(commits);
    }

    @PostMapping("/{owner}/{repo}/contents/{path:.+}")
    public ResponseEntity<Commit> createCommit(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo,
            @PathVariable("path") String path,
            @RequestParam("message") String message,
            @RequestParam(value = "branch", required = false) String branch,
            @RequestBody String content) {
        String accessToken = authHeader.replace("Bearer ", "");
        Commit commit = repositoryService.createCommit(accessToken, owner, repo, path, content, message, branch);
        return ResponseEntity.ok(commit);
    }

    @GetMapping("/{owner}/{repo}/branches")
    public ResponseEntity<List<String>> getRepositoryBranches(
            @RequestHeader("Authorization") String authHeader,
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo) {
        String accessToken = authHeader.replace("Bearer ", "");
        List<String> branches = repositoryService.getRepositoryBranches(accessToken, owner, repo);
        return ResponseEntity.ok(branches);
    }
}